package com.sinvo.oa.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sinvo.oa.entity.model.process.Process;
import com.sinvo.oa.entity.model.process.ProcessTemplate;
import com.sinvo.oa.entity.model.system.SysUser;
import com.sinvo.oa.service.ProcessService;
import com.sinvo.oa.service.ProcessTemplateService;
import com.sinvo.oa.service.SysUserService;
import com.sinvo.oa.service.WechatMessageService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.auth.AUTH;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Description
 */
@Service
public class WechatMessageServiceImpl implements WechatMessageService {

    @Autowired
    private ProcessService processService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ProcessTemplateService processTemplateService;

    @Autowired
    private WxMpService wxMpService;

    @Override
    public void pushPendingMessage(Long processId, Long userId, String taskId) {
        //根据id查询审批
        Process process = processService.getById(processId);
        //根据userid查询接收推送人信息
        SysUser receiver = sysUserService.getById(userId);
        //模板信息
        ProcessTemplate processTemplate = processTemplateService.getById(process.getProcessTemplateId());
        //提交人信息
        SysUser submitter = sysUserService.getById(process.getUserId());
        String openId = receiver.getOpenId();
        //TODO 测试使用
        if (StringUtils.isEmpty(openId)){
            openId = "oU3SF6qzpWp3oVIVpe0G8NbK7QqM";
        }
        //设置消息内容
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId) //openid
                .templateId("XfbgqGHck03TJDNzJ1b5UT7o1LXNqQKGt9iHdrP0vJU")
                .url("http://22b4c373.r8.cpolar.top/#/show/" + processId + "/" + taskId) //点击消息 跳转地址
                .build();
        JSONObject jsonObject = JSON.parseObject(process.getFormValues());
        JSONObject formShowData = jsonObject.getJSONObject("formShowData");
        StringBuffer content = new StringBuffer();
        for (Map.Entry entry : formShowData.entrySet()) {
            content.append(entry.getKey()).append("：").append(entry.getValue()).append("\n ");
        }
        //设置值
        templateMessage.addData(new WxMpTemplateData("first",submitter.getName()+"提交了"+processTemplate.getName()+"，请注意查看"));
        templateMessage.addData(new WxMpTemplateData("keyword1", process.getProcessCode()));
        templateMessage.addData(new WxMpTemplateData("keyword2", new DateTime(process.getCreateTime()).toString("yyyy-MM-dd HH:mm:ss")));
        templateMessage.addData(new WxMpTemplateData("content", content.toString()));

        //消息发送
        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println(msg);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}
